home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / joe014.zip / JOE014.TAZ / JOE014.tar / asyncbsd.c < prev    next >
C/C++ Source or Header  |  1992-01-23  |  6KB  |  309 lines

  1. /* Terminal interface for BSD
  2.    Copyright (C) 1991 Joseph H. Allen
  3.  
  4. This file is part of JOE (Joe's Own Editor)
  5.  
  6. JOE is free software; you can redistribute it and/or modify it under the terms
  7. of the GNU General Public License as published by the Free Software
  8. Foundation; either version 1, or (at your option) any later version. 
  9.  
  10. JOE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE.  See the GNU General Public License for more details.  
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with JOE; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <sgtty.h>
  19. #include <fcntl.h>
  20. #include <stdio.h>
  21. #include <signal.h>
  22. #include <sys/time.h>
  23. #include "async.h"
  24.  
  25. #define HZ 10            /* Clock ticks/second */
  26.  
  27. #define DIVISOR 11000000    /* The baud rate divided into this should
  28.                    give the number of microseconds per
  29.                    character.  It should attempt to
  30.                    reflect the true throughput, which is
  31.                    usually slower than the best possible
  32.                    for a given baud rate */
  33.  
  34. #define TIMES 3            /* Times per second that we check for
  35.                    typeahead */
  36.  
  37. static struct sgttyb oarg;
  38. static struct tchars otarg;
  39. static struct ltchars oltarg;
  40.  
  41. static unsigned char *obuf=0;
  42. static unsigned obufp=0;
  43. static unsigned obufsiz;
  44. static unsigned long ccc;
  45.  
  46. static unsigned speeds[]=
  47. {
  48. B50,50,B75,75,B110,110,B134,134,B150,150,B200,200,B300,300,B600,600,B1200,1200,
  49. B1800,1800,B2400,2400,B4800,4800,B9600,9600,EXTA,19200,EXTB,38400
  50. };
  51.  
  52. int tsignal();
  53.  
  54. sigjoe()
  55. {
  56. signal(SIGHUP,tsignal);
  57. signal(SIGTERM,tsignal);
  58. signal(SIGINT,SIG_IGN);
  59. signal(SIGPIPE,SIG_IGN);
  60. signal(SIGQUIT,SIG_IGN);
  61. }
  62.  
  63. signorm()
  64. {
  65. signal(SIGHUP,SIG_DFL);
  66. signal(SIGTERM,SIG_DFL);
  67. signal(SIGINT,SIG_DFL);
  68. signal(SIGPIPE,SIG_DFL);
  69. signal(SIGQUIT,SIG_DFL);
  70. }
  71.  
  72. aopen()
  73. {
  74. int x;
  75. struct sgttyb arg;
  76. struct tchars targ;
  77. struct ltchars ltarg;
  78. fflush(stdout);
  79. ioctl(fileno(stdin),TIOCGETP,&arg);
  80. ioctl(fileno(stdin),TIOCGETC,&targ);
  81. ioctl(fileno(stdin),TIOCGLTC,<arg);
  82. oarg=arg; otarg=targ; oltarg=ltarg;
  83. arg.sg_flags=( (arg.sg_flags&~(ECHO|CRMOD) ) | CBREAK) ;
  84. targ.t_intrc= -1;
  85. targ.t_quitc= -1;
  86. targ.t_eofc= -1;
  87. targ.t_brkc= -1;
  88. ltarg.t_suspc= -1;
  89. ltarg.t_dsuspc= -1;
  90. ltarg.t_rprntc= -1;
  91. ltarg.t_flushc= -1;
  92. ltarg.t_werasc= -1;
  93. ltarg.t_lnextc= -1;
  94. ioctl(fileno(stdin),TIOCSETN,&arg);
  95. ioctl(fileno(stdin),TIOCSETC,&targ);
  96. ioctl(fileno(stdin),TIOCSLTC,<arg);
  97. ccc=0;
  98. for(x=0;x!=30;x+=2)
  99.  if(arg.sg_ospeed==speeds[x])
  100.   {
  101.   ccc=DIVISOR/speeds[x+1];
  102.   break;
  103.   }
  104. if(obuf) free(obuf);
  105. if(!(TIMES*ccc)) obufsiz=4096;
  106. else
  107.  {
  108.  obufsiz=1000000/(TIMES*ccc);
  109.  if(obufsiz>4096) obufsiz=4096;
  110.  }
  111. if(!obufsiz) obufsiz=1;
  112. obuf=(unsigned char *)malloc(obufsiz);
  113. }
  114.  
  115. aclose()
  116. {
  117. aflush();
  118. ioctl(fileno(stdin),TIOCSETN,&oarg);
  119. ioctl(fileno(stdin),TIOCSETC,&otarg);
  120. ioctl(fileno(stdin),TIOCSLTC,&oltarg);
  121. }
  122.  
  123. int have=0;
  124. static unsigned char havec;
  125. static int yep;
  126.  
  127. static dosig()
  128. {
  129. yep=1;
  130. }
  131.  
  132. aflush()
  133. {
  134. if(obufp)
  135.  {
  136.  struct itimerval a,b;
  137.  unsigned long usec=obufp*ccc;
  138.  if(usec>=500000/HZ)
  139.   {
  140.   a.it_value.tv_sec=usec/1000000;
  141.   a.it_value.tv_usec=usec%1000000;
  142.   a.it_interval.tv_usec=0;
  143.   a.it_interval.tv_sec=0;
  144.   signal(SIGALRM,dosig);
  145.   yep=0;
  146.   sigsetmask(sigmask(SIGALRM));
  147.   setitimer(ITIMER_REAL,&a,&b);
  148.   write(fileno(stdout),obuf,obufp);
  149.   while(!yep) sigpause(0);
  150.   signal(SIGALRM,SIG_DFL);
  151.   }
  152.  else write(fileno(stdout),obuf,obufp);
  153.  obufp=0;
  154.  }
  155. if(!have)
  156.  {
  157.  fcntl(fileno(stdin),F_SETFL,FNDELAY);
  158.  if(read(fileno(stdin),&havec,1)==1) have=1;
  159.  fcntl(fileno(stdin),F_SETFL,0);
  160.  }
  161. }
  162.  
  163. unsigned char *take=0;
  164.  
  165. anext()
  166. {
  167. if(take)
  168.  if(*take)
  169.   {
  170.   int c;
  171.   if(*take!='\\') return *take++;
  172.   ++take;
  173.   if(!*take) return '\\';
  174.   else if(*take=='r') c='\r';
  175.   else if(*take=='b') c=8;
  176.   else if(*take=='n') c=10;
  177.   else if(*take=='f') c=12;
  178.   else if(*take=='a') c=7;
  179.   else if(*take=='\"') c='\"';
  180.   else if(*take>='0' && *take<='7')
  181.         {
  182.         c= *take++-'0';
  183.         if(*take>='0' && *take<='7')
  184.          {
  185.          c=c*8+*take++-'0';
  186.          if(*take>='0' && *take<='7') c=c*8+*take++-'0';
  187.          }
  188.         --take;
  189.         }
  190.   else c= *take;
  191.   ++take;
  192.   return c;
  193.   }
  194.  else take=0;
  195. aflush();
  196. if(have) have=0;
  197. else if(read(fileno(stdin),&havec,1)<1) tsignal(0);
  198. if(record) macroadd(havec);
  199. return havec;
  200. }
  201.  
  202. eputc(c)
  203. unsigned char c;
  204. {
  205. obuf[obufp++]=c;
  206. if(obufp==obufsiz) aflush();
  207. }
  208.  
  209. eputs(s)
  210. char *s;
  211. {
  212. while(*s)
  213.  {
  214.  obuf[obufp++]= *(s++);
  215.  if(obufp==obufsiz) aflush();
  216.  }
  217. }
  218.  
  219. getsize()
  220. {
  221. #ifdef TIOCGSIZE
  222. struct ttysize getit;
  223. #else
  224. #ifdef TIOCGWINSZ
  225. struct winsize getit;
  226. #else
  227. char *p;
  228. #endif
  229. #endif
  230. #ifdef TIOCGSIZE
  231. if(ioctl(fileno(stdout),TIOCGSIZE,&getit)!= -1)
  232.  {
  233.  if(getit.ts_lines>=3) height=getit.ts_lines;
  234.  if(getit.ts_cols>=2) width=getit.ts_cols;
  235.  }
  236. #else
  237. #ifdef TIOCGWINSZ
  238. if(ioctl(fileno(stdout),TIOCGWINSZ,&getit)!= -1)
  239.  {
  240.  if(getit.ws_row>=3) height=getit.ws_row;
  241.  if(getit.ws_col>=2) width=getit.ws_col;
  242.  }
  243. #else
  244. if(p=getenv("ROWS")) sscanf(p,"%d",&height);
  245. if(p=getenv("COLS")) sscanf(p,"%d",&width);
  246. if(height<3) height=24;
  247. if(width<2) width=80;
  248. #endif
  249. #endif
  250. }
  251.  
  252. termtype()
  253. {
  254. unsigned char entry[1024];
  255. unsigned char area[1024];
  256. unsigned char *foo=area;
  257. unsigned char *x=(unsigned char *)getenv("TERM");
  258. if(!x) goto down;
  259. if(tgetent(entry,x)!=1) goto down;
  260. height=tgetnum("li");
  261. if(height<3) height=24;
  262. width=tgetnum("co");
  263. if(width<2) width=80;
  264. if(!tgetstr("cs",&foo)) scroll=0;
  265. down:
  266. getsize();
  267. }
  268.  
  269. shell()
  270. {
  271. int x;
  272. char *s=(char *)getenv("SHELL");
  273. if(!s)
  274.  {
  275.  puts("\nSHELL variable not set");
  276.  return;
  277.  }
  278. eputs("\nYou are at the command shell.  Type 'exit' to continue editing\r\n");
  279. aclose();
  280. if(x=fork())
  281.  {
  282.  if(x!= -1) wait(0);
  283.  }
  284. else
  285.  {
  286.  signorm();
  287.  execl(s,s,0);
  288.  _exit(0);
  289.  }
  290. aopen();
  291. }
  292.  
  293. susp()
  294. {
  295. #ifdef SIGCONT
  296. eputs("\nThe editor has been suspended.  Type 'fg' to continue editing\r\n");
  297. yep=0;
  298. aclose();
  299. signal(SIGCONT,dosig);
  300. sigsetmask(sigmask(SIGCONT));
  301. kill(0,SIGTSTP);
  302. while(!yep) sigpause(0);
  303. signal(SIGCONT,SIG_DFL);
  304. aopen();
  305. #else
  306. shell();
  307. #endif
  308. }
  309.